// Ionic Starter App

// angular.module is a global place for creating, registering and retrieving Angular modules
// 'starter' is the name of this angular module example (also set in a <body> attribute in index.html)
// the 2nd parameter is an array of 'requires'
angular.module('starter', ['ionic'])

    .run(function ($ionicPlatform) {
        $ionicPlatform.ready(function () {
            if (navigator.splashscreen) {
                navigator.splashscreen.hide();
            }
        });
    })
    .controller('TestController', function ($scope, ExpertService) {
        $scope.devices = [];
        $scope.device = {};
        $scope.scan = function scan() {
            console.debug('scan...');
            $scope.devices = [];
            ExpertService.scan().then(function (devices) {
                $scope.devices = devices;
                $scope.error = null;
            }, function (reason) {
                $scope.devices = [];
                $scope.error = JSON.stringify(reason);
            });
        };
        $scope.connect = function connect(device) {
            $scope.device = device;
            ExpertService.connect(device).then(function () {
                console.debug("device.connected: " + device.connected);
            });
        }
        $scope.disconnect = function disconnect(device) {
            ExpertService.disconnect(device).then(function(){
                console.debug('disconnected.');
            });
        }
        // $scope.readVersion = function readVersion(device) {
        //   ExpertService.readVersion(device.id).then(function(data){
        //     console.debug(JSON.stringify(data));
        //     device.version = data;
        //   });
        // }
        $scope.stopRead = function stopRead(device) {
            ExpertService.stopRead(device).then(function(){
                console.debug(device);
                // device.reading = false;
            });
        }
        $scope.startReadTemperature = function startReadTemperature(device) {
            ExpertService.startReadTemperature(device, function (data) {
                console.debug(1, JSON.stringify(data));
                $scope.$apply(function(){
                    // console.debug("device", JSON.stringify(device));
                    // device.value_ = data;
                    angular.extend(device, data);
                });
            });
        }
        $scope.startReadRevolution = function startReadRevolution(device) {
            ExpertService.startReadRevolution(device, function (data) {
                // console.debug(1, JSON.stringify(data));
                $scope.$apply(function(){
                    // console.debug("device", JSON.stringify(device));
                    // device.value_ = data;
                    angular.extend(device, data);
                });
            });
        }
        $scope.startReadAcceleration = function startReadAcceleration(device) {
            ExpertService.startReadAcceleration(device, function (data) {
                // console.debug(1, JSON.stringify(data));
                $scope.$apply(function(){
                    // console.debug("device", JSON.stringify(device));
                    // device.value_ = data;
                    angular.extend(device, data);
                });
            });
        }
        $scope.startReadSpeed = function startReadSpeed(device) {
            ExpertService.startReadSpeed(device, function (data) {
                // console.debug(1, JSON.stringify(data));
                $scope.$apply(function(){
                    // console.debug("device", JSON.stringify(device));
                    // device.value_ = data;
                    angular.extend(device, data);
                });
            });
        }
        $scope.startReadDisplacement = function startReadDisplacement(device) {
            ExpertService.startReadDisplacement(device, function (data) {
                console.debug(1, JSON.stringify(data));
                $scope.$apply(function(){
                    // console.debug("device", JSON.stringify(device));
                    angular.extend(device, data);
                });
            });
        }
    })
